package test2zero;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextBox;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.sun.javafx.application.PlatformImpl;
public class FxView extends ViewPart{

	public static final String ID ="Test2Zero.fxview";

	private static java.awt.Panel jPanel1;
	static Frame locationFrame;

	private static JFXPanel jfxPanel;  

	static Rectangle rect;
	static BorderPane border;

	protected  VBox vb = null;
	protected static Group root;
	protected static Scene scene;
	protected  TextBox  tb;

	public void createPartControl(final Composite parent) {
		final Composite composite = new Composite(parent, SWT.EMBEDDED);
		//composite.setBounds(20, 20, 300, 200);
		//composite.setLayout(new RowLayout( ));

		locationFrame = SWT_AWT.new_Frame(composite);
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				jPanel1 = new java.awt.Panel();
				jPanel1.setLayout(new java.awt.BorderLayout());
				locationFrame.add(jPanel1);
			}
		});


		// Create JavaFX panel.
		// JFXPanel component by Artem Ananiev, 
		//which allows for embedding JavaFX nodes into a Swing application.
		jfxPanel = new JFXPanel(); 
		//jfxPanel.setPreferredSize(new Dimension(550, 400));
		//border = new BorderPane();

		PlatformImpl.startup(new Runnable() {  
			public void run() {  
				Stage stage = new Stage();
				stage.setTitle("Test JFX");
				stage.centerOnScreen();
				
				Scene scene = createScene();
				stage.setScene(createScene());
				stage.setVisible(true);
				
				
				//Scene scene = getUI(); //only cube draggable
				jfxPanel.setScene(scene);
				
				//jfxPanel.setScene(draggableRectCube());
				
			}});

		locationFrame.add(jfxPanel, BorderLayout.CENTER);
		locationFrame.pack();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	//Layout 
	public Scene createScene(){
		HBox hbox = new HBox(20);
		hbox.setTranslateX(20);
		hbox.setTranslateY(20);

		SplitPane splitPane1 = new SplitPane();
		splitPane1.setOrientation(Orientation.VERTICAL);
		splitPane1.setPrefSize(400, 400);


		final Button l1 = new Button("Left Button");
		final Button r1 = new Button("Right Button");

		//add cube and rectangle
		//splitPane1.getItems().addAll(draggableRectNode(Color.ROYALBLUE), draggableRectCube());
		
		splitPane1.getItems().addAll(draggableRectNode(Color.ROYALBLUE), draggableRectNode(Color.FUCHSIA));
		//splitPane1.getItems().addAll(draggableRectCube());
		//splitPane1.getItems().addAll(draggableRectCube());
		hbox.getChildren().add(splitPane1);

		SplitPane splitPane2 = new SplitPane();
		splitPane2.setOrientation(Orientation.VERTICAL);
		splitPane2.setPrefSize(300, 200);
		final Button l2 = new Button("Left Button");
		final Button c2 = new Button("Center Button");
		final Button r2 = new Button("Right Button");
		splitPane2.getItems().addAll(l2, c2, r2);
		hbox.getChildren().add(splitPane2);

		Scene scene = new Scene(new Group(hbox), 560, 240);
		scene.setFill(Color.GHOSTWHITE);
		scene.setCamera(new PerspectiveCamera());
		
		
		return scene;
		//stage.setScene(scene);
	}
	
	
	//Draggable node
	//Returs the Scene with Draggable Node
	public Scene getUI(){
		BorderPane border = new BorderPane();
		border.setTop(new Rectangle(200, 50, Color.DARKCYAN));
		border.setBottom(new Rectangle(200, 50, Color.DARKCYAN));
		border.setCenter(new Rectangle(100, 100, Color.MEDIUMAQUAMARINE));
		border.setLeft(new Rectangle(50, 100, Color.DARKTURQUOISE));
		border.setRight(new Rectangle(50, 100, Color.DARKTURQUOISE));

		HBox hbox = new HBox();
		hbox.setPadding(new Insets(15, 12, 15, 12));
		hbox.setSpacing(10);
		hbox.setStyle("-fx-background-color: #336699");
		//	DraggableNode dragNode =  draggableRectNode();
		//hbox.getChildren().add(draggableNode());

		border.setTop(hbox);

		//Making a DraggableNode Object
		DraggableNode panel = new DraggableNode();

		//Defining style class for this node
		// panel.getStyleClass().add("rect");
		Rectangle rect = new Rectangle();
		rect.setFill(Color.RED);
		rect.setWidth(200);
		rect.setHeight(100);
		
		rect.setX(20.0f);
		rect.setY(20.0f);

		DropShadow ds = new DropShadow();
		ds.setOffsetY(5.0);
		ds.setOffsetX(5.0);
		ds.setColor(Color.GRAY);

		Reflection reflection = new Reflection();

		ds.setInput(reflection);    
		rect.setEffect(ds);

		//Cube
		Cube cube = new Cube(50,Color.RED,1);
		cube.rx.setAngle(45);
		cube.ry.setAngle(45);

		// cube.setEffect(ds);

		Cube c2 = new Cube(50,Color.GREEN,1);
		c2.setTranslateX(100);
		c2.rx.setAngle(45);
		c2.ry.setAngle(45);

		Cube c3 = new Cube(50,Color.ORANGE,1);
		c3.setTranslateX(-100);
		c3.rx.setAngle(45);
		c3.ry.setAngle(45);

		Timeline animation = new Timeline();
		animation.getKeyFrames().addAll(
				new KeyFrame(Duration.ZERO,
						new KeyValue(cube.ry.angleProperty(), 0d),
						new KeyValue(c2.rx.angleProperty(), 0d),
						new KeyValue(c3.rz.angleProperty(), 0d)
				),
				new KeyFrame(Duration.valueOf(1000),
						new KeyValue(cube.ry.angleProperty(), 360d),
						new KeyValue(c2.rx.angleProperty(), 360d),
						new KeyValue(c3.rz.angleProperty(), 360d)
				));
		animation.setCycleCount(Animation.INDEFINITE);
		// create root group
		Group root = new Group(cube,c2,c3);
		// translate and rotate group so that origin is center and +Y is up
		root.setTranslateX(400/2);
		root.setTranslateY(150/2);
		root.getTransforms().add(new Rotate(180,Rotate.X_AXIS));
		// create scene
		//Scene scene = new Scene(root, 400,150);

		panel.getChildren().add(cube);
		Scene scene = new Scene(panel, 400,150);
		//hbox.getChildren().addAll(cube , rect);

		//Scene scene = new Scene(panel, 400,150);
		//Scene scene = new Scene(dragNode, 400,150);
		scene.setCamera(new PerspectiveCamera());
		// stage.setScene(scene);
		//stage.setVisible(true);
		// start spining animation
		//animation.play();
		//

		Rectangle rectViolet = new Rectangle();
		rectViolet.setFill(Color.VIOLET);
		rectViolet.setWidth(200);
		rectViolet.setHeight(100);
		rectViolet.setX(20.0f);
		rectViolet.setY(20.0f);

		DropShadow ds1 = new DropShadow();
		ds1.setOffsetY(5.0);
		ds1.setOffsetX(5.0);
		ds1.setColor(Color.GRAY);

		Reflection reflection1 = new Reflection();

		ds.setInput(reflection1);    
		rectViolet.setEffect(ds1);
		///
		/* Rectangle rectangle=new Rectangle(20,20);
	        rectangle.setFill(Color.BLUEVIOLET);
	        rectangle.setWidth(200);
	        rectangle.setHeight(50);
	        rectangle.setX(20.0f);
	        rectangle.setY(20.0f);
	        rectangle.setLayoutX(20.0f);

	        DropShadow ds = new DropShadow();
	        ds.setOffsetY(4.0f);
	        ds.setOffsetX(4.0f);
	        ds.setColor(Color.CORAL);

	        Reflection reflection = new Reflection();
	       reflection.setFraction(0.9);

	       // ds.setInput(reflection);    
	        rectangle.setEffect(reflection);
	        //rectangle.setEffect(r);
		 */	       
		//okk panel.getChildren().add(rect);
		//panel.getChildren().add(root);
		//hbox.getChildren().add(panel);
		//panel.getChildren().add(rectViolet);
		//Scene scene = new Scene(panel, 640, 480);

		//using a css for styling controls
		// scene.getStylesheets().add("/javafxapplication/main.css");
		return scene;
	}

	private DraggableNode draggableRectNode(final Color color){
		//Making a DraggableNode Object
		DraggableNode panel = new DraggableNode();

		Rectangle rect = new Rectangle();
		rect.setFill(color);
		rect.setWidth(200);
		rect.setHeight(100);
		rect.setX(20.0f);
		rect.setY(20.0f);

		DropShadow ds = new DropShadow();
		ds.setOffsetY(5.0);
		ds.setOffsetX(5.0);
		ds.setColor(Color.GRAY);

		Reflection reflection = new Reflection();

		ds.setInput(reflection);    
		rect.setEffect(ds);
		panel.getChildren().add(rect);
		return panel;
	}


	private DraggableNode draggableRectCube(){
		//Making a DraggableNode Object
		DraggableNode panel = new DraggableNode();

		//Cube
		Cube cube = new Cube(50,Color.RED,1);
		cube.rx.setAngle(45);
		cube.ry.setAngle(45);

		// cube.setEffect(ds);

		Cube c2 = new Cube(50,Color.GREEN,1);
		c2.setTranslateX(100);
		c2.rx.setAngle(45);
		c2.ry.setAngle(45);

		Cube c3 = new Cube(50,Color.ORANGE,1);
		c3.setTranslateX(-100);
		c3.rx.setAngle(45);
		c3.ry.setAngle(45);

		Timeline animation = new Timeline();
		animation.getKeyFrames().addAll(
				new KeyFrame(Duration.ZERO,
						new KeyValue(cube.ry.angleProperty(), 0d),
						new KeyValue(c2.rx.angleProperty(), 0d),
						new KeyValue(c3.rz.angleProperty(), 0d)
				),
				new KeyFrame(Duration.valueOf(1000),
						new KeyValue(cube.ry.angleProperty(), 360d),
						new KeyValue(c2.rx.angleProperty(), 360d),
						new KeyValue(c3.rz.angleProperty(), 360d)
				));
		animation.setCycleCount(Animation.INDEFINITE);
		// create root group
		Group root = new Group(cube,c2,c3);
		// translate and rotate group so that origin is center and +Y is up
		root.setTranslateX(400/2);
		root.setTranslateY(150/2);
		root.getTransforms().add(new Rotate(180,Rotate.X_AXIS));
		// create scene
		//Scene scene = new Scene(root, 400,150);

		panel.getChildren().add(cube);
		//Scene scene = new Scene(panel, 400,150);
		//scene.setCamera(new PerspectiveCamera());
		// start spining animation
		//animation.play();
		//

		return panel;
	}
}

